//
//  VideoPlayerViewController.m
//  IFengNews
//
//  Created by lanou3g on 16/3/8.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "VideoPlayerViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface VideoPlayerViewController ()

@property (nonatomic,strong) MPMoviePlayerController *moviePlayer;
//@property (nonatomic,strong) MPMoviePlayerViewController *moviePlayerVC;
@end

@implementation VideoPlayerViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.moviePlayer play];
    [self addNofi];
    
}
//点击屏幕暂停
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //写了pause后系统会自动将自带的视频播放的一些按钮展示在屏幕上，例如进度条、暂停按钮
    [self.moviePlayer pause];
}
//添加视频播放的通知
- (void)addNofi
{
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackstateDidChange:)name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];//播放状态改变，可配合playbakcState属性获取具体状态
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playDidFinish:)name:MPMoviePlayerPlaybackDidFinishNotification object:nil];//媒体播放完成或用户手动退出，具体完成原因可以通过通知userInfo中的key为MPMoviePlayerPlaybackDidFinishReasonUserInfoKey的对象获取
}
/*
//- (void)playbackstateDidChange:(NSNotification *)noti
//{
//    switch (self.moviePlayer.playbackState) {
//            
//        case MPMoviePlaybackStateInterrupted:
////中断
//            NSLog(@"中断");
//            break;
//        case MPMoviePlaybackStatePaused:
////暂停
//            NSLog(@"暂停");
//            break;
//        case MPMoviePlaybackStatePlaying:
////播放中
//            NSLog(@"播放中");
//            break;
//        case MPMoviePlaybackStateSeekingBackward:
////后退
//            NSLog(@"后退");
//            break;
//        case MPMoviePlaybackStateSeekingForward:
////快进
//            NSLog(@"快进");
//            break;
//        case MPMoviePlaybackStateStopped:
////停止
//            NSLog(@"停止");
//            break;
//            
//        default:
//            break;
//    }
//}
 */
- (void)playDidFinish:(NSNotification *)noti
{
    //播放完成
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 懒加载
- (MPMoviePlayerController *)moviePlayer
{
    if (!_moviePlayer) {
        _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:self.videoUrl]];
        _moviePlayer.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 60);
        _moviePlayer.scalingMode = MPMovieScalingModeNone; //固定缩放比例并且尽量全部
        [self.view addSubview:_moviePlayer.view];
    }
    return _moviePlayer;
}
@end
